<?php
class VideoEncoderComponent extends Object {
	var $files;
	/**
	 * Everything in this method can be placed into a global configuration
	 * file that is called at bootstrap/runtime.
	 **/
	function __construct () {
		// ffmpeg path
		Configure::write('Video.ffmpeg_path', 'ffmpeg');
		// flvtool2 path
		Configure::write('Video.flvtool2_path', ROOT.DS.'app'.DS.'webroot'.DS.'files'.DS.'flvtool2');
	}
	

	function convert_2_mpeg ($in, $out, $width = 640, $height = 350){		
		$command = Configure::read('Video.ffmpeg_path') . " -i {$in}  -r 30  -ar 44100 -acodec libmp3lame -ac 2 -ab 192k -bt 4000k -b 800k  {$out}";
		return @exec($command);
	}
	
	function convert_2_h264 ($in, $out, $width = 640, $height = 350){	
		$command = Configure::read('Video.ffmpeg_path') . " -i {$in} -s {$width}x{$height} -r 30 -ar 44100 -acodec libfaac  -vcodec libx264 -ac 2 -ab 192k  -bt 4000k -b 800k  {$out}";
		return @exec($command);
	}
	
	
	
	function join_video($in = 'tmp_%%%.mpg',$out){
		$command = "cat {$in} > {$out}";
		return @exec($command);
	}
	
	function join_image_to_video($in = 'tmp_000.jpg',$sound,$out,$width = 640,$height = 350){
		// z dzwiękiem
		$command = Configure::read('Video.ffmpeg_path') ." -loop_input -vframes 75 -i {$in} -i {$sound} -acodec libmp3lame -y -r 25 -ac 2 -ab 64k -b 800k -s {$width}x{$height} {$out}";		
		return @exec($command);
		
	}
	
	function set_buffering ($in) {
		$command = Configure::read('Video.flvtool2_path') . " -U {$in}";
		shell_exec($command);
	}
	
	function grab_image ($in, $out,$width = 640,$height = 350) {
		$command = Configure::read('Video.ffmpeg_path') . " -y -i {$in} -f mjpeg -t 0.003 {$out}";
//		$command = Configure::read('Video.ffmpeg_path').' -v 0 -y -i '.$in." -vframes 1 -ss 5 -vcodec mjpeg -f rawvideo -s {$width}x{$height} -aspect 16:9 ".$out;
		return shell_exec($command);
	}
	
	function grab_part_audio(){
		"ffmpeg -ss 00:00:30.00 -t 25 -i foo.mp3 -ab 256k foo-new.mp3";
		"ffmpeg -ss 00:00:30.00 -t 25 -i bar.mp3 -acodec copy bar-new.mp3";
		$ffmpeg = "ffmpeg -ss 00:00:3.00 -t 25 -i bar.mp3 -acodec libmp3lame bar-new.mp3";
		$command = Configure::read('Video.ffmpeg_path') . $ffmpeg;
		return shell_exec($command);
	}
	
	function get_duration ($in) {
		$command = Configure::read('Video.ffmpeg_path') . " -i {$in} 2>&1 | grep \"Duration\" | cut -d ' ' -f 4 | sed s/,//";
		return shell_exec($command);
	}
	
	function get_filesize ($in) {
		return filesize($in);
	}
	
	function remove_uploaded_video ($in, $ext) {
		$files = $this->get_files($in,$ext);
		if(is_array($files)){
			foreach ($files as $key => $filename) {
				@unlink($filename);
			}
		}
	}
	
	function get_files($target,$ext){
		$exceptions=array('.','..');
		$pattern="(tmp_(.*).{$ext}$)";
//		$pattern="((.*)$)";
		if (!$sourcedir=@opendir($target)) return false;
		$i=0;
		while(false!==($sibling=readdir($sourcedir))){
			if(!in_array($sibling,$exceptions)){
				$object=str_replace('//','/',$target.'/'.$sibling);
				if(is_dir($object)){
					$this->get_files($object,$file);
				}
				
				if(is_file($object) && preg_match($pattern,$object)){
					$this->files[$i] = $object;
				}
				$i++;
			}
		}
		closedir($sourcedir);
		return $this->files;	
	}
	
	function create_image_text($text,$filename){ 

	    //Set the image width and height 
	    $width = 520; 
	    $height = 350;  

		$font = WWW_ROOT . 'files' . DS . 'fonts' . DS.'arial.ttf'; 
		


	    //Create the image resource 
	    $image = ImageCreate($width, $height);  
	
	    //We are making three colors, white, black and gray 
	    $white = ImageColorAllocate($image, 255, 255, 255); 
	    $black = ImageColorAllocate($image, 0, 0, 0); 
	    $grey = ImageColorAllocate($image, 204, 204, 204); 
	
	    //Make the background black 
	    ImageFill($image, 0, 0, $black); 
	
	    //Add randomly generated string in white to the image
//	    ImageString($image, 3, 30, 3, $text, $white); 
	
	    //Throw in some lines to make it a little bit harder for any bots to break 
//	    ImageRectangle($image,0,0,$width-1,$height-1,$grey);
	    
//	    $nl = (substr($_SERVER['OS'], 'Windows') !== FALSE) ? '\r\n' : '\n'; // Unix;
	    $nl = "\n";
		$rows = split($nl, $text);
		$row = 	1;
		$litery = 40;
		
		foreach ($rows as $key => $message) {
			$text = str_split($message,$litery);
			foreach ($text as $i => $value) {
				$row++;
			}
		}		
		//Set starting point for drawing 
		$row = ( $row >= 8) ? 8 : $row;
		$top = (((10 - $row))/2)* 35 ;
		$y = 50;	
		$row = 	1;
		foreach ($rows as $key => $message) {
			$text = str_split($message,$litery);
			foreach ($text as $i => $value) {
				$row_text = trim($value);
				$x = round((520-((strlen($row_text)*480)/$litery))/2,0);
				ImageTTFText($image, 20, 0, $x, $top + (($y - 16)*($row)), $white, $font, $row_text);
				$row++;
			}
		}
	    
	     
//	    imageline($image, 0, $height/2, $width, $height/2, $grey); 
//	    imageline($image, $width/2, 0, $width/2, $height, $grey); 
	 
	    //Output the newly created image in jpeg format 
	    @ImageJpeg($image,$filename); 
	    
	    //Free up resources
	    ImageDestroy($image); 
	}
	
	/**
	 * 
	 * Encode a video sequence for the iPpod/iPhone
	 */
	function encode_for_iphone(){
	 	
		$command = 'ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4';

		//Explanations :
		//Source : source_video.avi
		//Audio codec : aac
		//Audio bitrate : 128kb/s
		//Video codec : mpeg4
		//Video bitrate : 1200kb/s
		//Video size : 320px par 180px
		//Generated video : final_video.mp4
	}
}